package com.example.prison.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.config.ApiUtil;
import com.example.prison.config.LoginInterceptor;
import com.example.prison.entity.LeaveManagement;
import com.example.prison.entity.vo.PeopleDestination;
import com.example.prison.mapper.LeaveManagementMapper;
import com.example.prison.mapper.UserInfoMapper;
import com.example.prison.service.LeaveManagementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author jiang
* @description 针对表【leave_management】的数据库操作Service实现
* @createDate 2023-04-06 15:18:51
*/
@Service
public class LeaveManagementServiceImpl extends ServiceImpl<LeaveManagementMapper, LeaveManagement>
    implements LeaveManagementService {

    @Autowired
    private LeaveManagementMapper leaveManagementMapper;

//    @Autowired
//    private PersonAlarmRecordMapper personAlarmRecordMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /** 获取请假列表 */
    @Override
    public PageInfo<LeaveManagement> getLeaves(int pageNumber, int pageSize, String destination, String status, String beginTime, String endTime) {
        List<LeaveManagement> list = leaveManagementMapper.getLeaves(destination, status, beginTime, endTime);
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        for(LeaveManagement leave : list) {
            if(date.compareTo(leave.getBeginDate()) >= 0 && Objects.equals(leave.getStatus(), "同意")) {
                leave.setStatus("未销假");
                leaveManagementMapper.updateStatus(leave);
            }
        }
//        if(Objects.equals(LoginInterceptor.loginUser.getRoleId(), "普通用户")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(leaveManagementMapper.getOneListByName(LoginInterceptor.loginUser.getUserName()));
//        } else
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(leaveManagementMapper.getOneListByLoginDepart(LoginInterceptor.loginUser.getDepartment()));
//        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(leaveManagementMapper.getLeaves(destination, status, beginTime, endTime));
    }

    @Override
    public int addLeave(LeaveManagement leave) {
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员") && !Objects.equals(LoginInterceptor.loginUser.getDepartment(), leave.getDepart())) {
//            return 0;
//        }
        leave.setComments("");
        leave.setStatus("未审核");
        List<LeaveManagement> leavesList = leaveManagementMapper.getOneLeaveByNameAndDepart(leave.getName(),leave.getDepart());
        for(LeaveManagement leaveOneName : leavesList) {
            if (ObjectUtils.isNotEmpty(leaveOneName)) {
                int beginTimeResult = leave.getBeginDate().compareTo(leaveOneName.getBeginDate());
                int endTimeResult = leave.getEndDate().compareTo(leaveOneName.getEndDate());
                int beginToEnd = leave.getBeginDate().compareTo(leaveOneName.getEndDate());
                int endToBegin = leave.getEndDate().compareTo(leaveOneName.getBeginDate());
                if ((endToBegin >= 0 && endTimeResult <= 0) || (beginTimeResult >= 0 && beginToEnd <= 0) || (beginTimeResult <= 0 && endTimeResult >= 0)) {
                    return -1;
                }
            }
        }
        leaveManagementMapper.addLeave(leave);
        return 1;
    }

    @Override
    public void deleteLeave(Integer[] ids) {
        leaveManagementMapper.deleteLeave(ids);
    }

    @Override
    public void deleteLeaveById(Integer id) {
        leaveManagementMapper.deleteLeaveById(id);
    }

    @Override
    public Integer checkLeave(LeaveManagement leave) {
        LeaveManagement leaveManagement = leaveManagementMapper.getLeavesById(leave.getId());
        if(Objects.equals(leaveManagement.getStatus(), "未审核")) {
            leaveManagementMapper.checkLeave(leave);
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public LeaveManagement getLeavesById(Integer id) {
        return leaveManagementMapper.getLeavesById(id);
    }

    /**
     * 打卡测试接口
     */
    @Override
    public List<LeaveManagement> getLeavesByNameAndStatus(String name, String status) {
        return leaveManagementMapper.getLeavesByNameAndStatus(name,status);
    }

    /** 依据状态获取请假列表 */
    @Override
    public PageInfo<LeaveManagement> getLeavesByStatus(int pageNumber, int pageSize, String status) {
//        if(Objects.equals(LoginInterceptor.loginUser.getRoleId(), "普通用户")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(leaveManagementMapper.getLeavesByStatusAndLogin(status,LoginInterceptor.loginUser.getUserName()));
//        } else
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(leaveManagementMapper.getLeavesByStatusAndLoginDepart(status,LoginInterceptor.loginUser.getDepartment()));
//        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(leaveManagementMapper.getLeavesByStatus(status));
    }

    /** 依据时间获取请假列表 */
    @Override
    public PageInfo<LeaveManagement> getLeavesByTime(int pageNumber, int pageSize, String beginDate, String endDate) {
//        if(Objects.equals(LoginInterceptor.loginUser.getRoleId(), "普通用户")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(leaveManagementMapper.getLeavesByTimeAndLogin(beginDate, endDate,LoginInterceptor.loginUser.getUserName()));
//        } else
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(leaveManagementMapper.getLeavesByTimeAndLoginDepart(beginDate, endDate,LoginInterceptor.loginUser.getDepartment()));
//        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(leaveManagementMapper.getLeavesByTime(beginDate, endDate));
    }

    @Override
    public Integer updateLeave(LeaveManagement leave) {
        LeaveManagement leaveManagement = leaveManagementMapper.getLeavesById(leave.getId());
        if(Objects.equals(leaveManagement.getStatus(), "未审核")) {
            leaveManagementMapper.updateLeave(leave);
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public Integer updateStatus(LeaveManagement leave){
        LeaveManagement leaveManagement = leaveManagementMapper.getLeavesById(leave.getId());
        if(Objects.equals(leaveManagement.getStatus(), "同意")) {
            leave.setStatus("未销假");
            leaveManagementMapper.updateStatus(leave);
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public Integer earlyEndLeave(LeaveManagement leave){
        LeaveManagement leaveManagement = leaveManagementMapper.getLeavesById(leave.getId());
        if(Objects.equals(leaveManagement.getStatus(), "未销假")) {
            leave.setStatus("已销假");
            leaveManagementMapper.earlyEndLeave(leave);
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public Integer endLeave(LeaveManagement leave){
        LeaveManagement leaveManagement = leaveManagementMapper.getLeavesById(leave.getId());
        if(Objects.equals(leaveManagement.getStatus(), "未销假")) {
            leave.setStatus("已销假");
            leaveManagementMapper.endLeave(leave);
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public PageInfo<LeaveManagement> getNoReturn(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
        List<LeaveManagement> list = leaveManagementMapper.getNoReturn(date);
        for(LeaveManagement i : list) {
//            if(ObjectUtils.isEmpty(personAlarmRecordMapper.getOneNoReturnByNameAndDepart(i.getName(),i.getDepart()))) {
//                //到期未归告警
//                PersonAlarmRecord record = new PersonAlarmRecord();
//                record.setType("到期未归");
//                record.setTime(date);
//                record.setName(i.getName());
//                record.setDepart(i.getDepart());
//                record.setDestination(i.getDestination());
//                record.setSolve("未处理");
//                personAlarmRecordMapper.add(record);
//            }
        }
        return new PageInfo<>(leaveManagementMapper.getNoReturn(date));
    }

    @Override
    public PageInfo<LeaveManagement> getLongTime(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        String date = ApiUtil.dayProcess(-45);//当前时间减去45天
        List<LeaveManagement> list = leaveManagementMapper.getLongTime(date);
        for(LeaveManagement i : list) {
//            if(ObjectUtils.isEmpty(personAlarmRecordMapper.getOneNoReturnByNameAndDepart(i.getName(),i.getDepart()))) {
//                //长期预警
//                PersonAlarmRecord record = new PersonAlarmRecord();
//                record.setType("长期预警");
//                record.setTime(date);
//                record.setName(i.getName());
//                record.setDepart(i.getDepart());
//                record.setDestination(i.getDestination());
//                record.setSolve("未处理");
//                personAlarmRecordMapper.add(record);
//            }
        }
        return new PageInfo<>(leaveManagementMapper.getLongTime(date));
    }

    /** 依据请假去向获取请假列表 */
    @Override
    public PageInfo<LeaveManagement> getLeaveByDestination(int pageNumber, int pageSize, String destination) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(leaveManagementMapper.getLeaveByDestination(destination));
    }

    @Override
    public List<PeopleDestination> getLeaveDestinationAndPeople() {
        List<LeaveManagement> leaveList = leaveManagementMapper.getLeaving();
        Map<String, List<LeaveManagement>> map = new HashMap<>();
        for(LeaveManagement leave : leaveList) {
            String[] des = leave.getDestination().split("-");
            List<LeaveManagement> destinationList = leaveManagementMapper.getLeavingByDestination(des[0]);
            map.put(des[0],destinationList);
        }
        List<PeopleDestination> peopleList = new ArrayList<>();
        Set<String> keyList = map.keySet();
        for(String key : keyList) {
            PeopleDestination destination = new PeopleDestination();
            destination.setDestination(key);
            destination.setList(map.get(key));
            destination.setSize(map.get(key).size());
            peopleList.add(destination);
        }
        return peopleList;
    }


}




